<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $企业微信应用API
 * core_open_plugin 第三方应用接口，注意区分PID，一个是系统插件ID
 * core_account_permissions 里面的PID，是系统ID
*/
namespace core;
defined('BY_JYA') or exit('error');
class wxwork_app{
	/*
		auth核实企业身份（非普通客户）
	*/
	public function checkauth($plugin_name=''){
		if((!$_SESSION['open_userid'] && !$_SESSION['openid']) || !$_SESSION['uid'] || $_SESSION['open_userid']=='isget'){
			exi('','','core/member.wxwork_auth//plugin_name='.$plugin_name);
		}
		return true;
	}
	/*
		获取企业永久授权码 返回的信息
	*/
	public function upitem($merchid=0,$content=array(),$pluginname='',$pid=0){
		$merchid=$merchid?:$_SESSION['onemerchid'];
		$wxwork=pdo_get('core_account_wxwork',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		$data=array(
			'title'=>$content['auth_corp_info']['corp_name']?:$wxwork['title'],
			'logo'=>$content['auth_corp_info']['corp_square_logo_url']?:$wxwork['logo'],
			'appid'=>trim($content['auth_corp_info']['corpid']),
		);
		if($id){
			pdo_update('core_account_wxwork',$data,array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['merchid']=$merchid;
			$data['createtime']=TIMESTAMP;
			pdo_insert('core_account_wxwork',$data);
		}
		$pid=$pid?:pdo_getcolumn('core_plugins',array('name'=>$pluginname),'id');
		$wxwork_app=pdo_get('core_account_wxwork_app',array('pid'=>$pid,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		$data=array(
			'title'=>$content['auth_info']['agent'][0]['name']?:$wxwork_app['title'],
			'logo'=>$content['auth_info']['agent'][0]['square_logo_url']?:$wxwork_app['logo'],
			'appid'=>$content['auth_info']['agent'][0]['agentid']?:$wxwork_app['appid'],
		);
		if($id){
			pdo_update('core_account_wxwork_app',$data,array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['merchid']=$merchid;
			$data['createtime']=TIMESTAMP;
			pdo_insert('core_account_wxwork_app',$data);
		}
	}
}